package com.wsk.config;

import org.springframework.beans.factory.annotation.Value;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @Description: 发送验证码
 * @Author: yh
 * @date: 2023/04/26 14:54
 **/
public class MailSender {
    private static final String SMTP_HOST = "smtp.qq.com"; // QQ邮箱SMTP服务器地址

    public static void sendVerificationCode(String username,String password,String recipient, String text) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", SMTP_HOST);
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.ssl.enable", "true");

        Session session = Session.getInstance(props);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        message.setSubject("Verification Code");
        message.setText(text);

        Transport transport = session.getTransport("smtp");
        transport.connect(SMTP_HOST, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
